#!/bin/bash

set -e

# Commands: ifconfig, netstat, route ...
if ! rpm -q net-tools; then
   echo "install yum-utils..."
   rpm -ivh DS-RPMS/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm
fi

# Check install status
if rpm -qa | grep onlyoffice-documentserver; then
    echo "documentserver already installed"
    exit
elif netstat -lnp | awk '{print $4}' | grep -qE ":8083$|:5432$|:5672$|:6379$|:8000$|:8080$"; then
   cat<<EOF
documentserver want ports:
   8083 -> nginx
   5432 -> postgres
   5672 -> rabbitmq
   6379 -> redis
   8000 -> docservice
   8080 -> spellchecker
EOF
   exit
fi

# Don not change the order, mabe not work

echo "install yum-utils..."
rpm -ivh DS-RPMS/python-chardet-2.2.1-3.el7.noarch.rpm \
         DS-RPMS/libxml2-python-2.9.1-6.el7.4.x86_64.rpm \
         DS-RPMS/python-kitchen-1.1.1-5.el7.noarch.rpm \
         DS-RPMS/yum-utils-1.1.31-54.el7_8.noarch.rpm || true

echo "install expect..."
rpm -ivh DS-RPMS/tcl-8.5.13-8.el7.x86_64.rpm \
         DS-RPMS/expect-5.45-14.el7_1.x86_64.rpm

echo "install-1 wget..."
rpm -ivh DS-RPMS/wget-1.14-18.el7_6.1.x86_64.rpm

echo "install-2 supervisor..."
rpm -ivh DS-RPMS/python-backports-1.0-8.el7.x86_64.rpm \
         DS-RPMS/python-ipaddress-1.0.16-2.el7.noarch.rpm \
         DS-RPMS/python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm \
         DS-RPMS/python-setuptools-0.9.8-7.el7.noarch.rpm \
         DS-RPMS/python-meld3-0.6.10-1.el7.x86_64.rpm \
         DS-RPMS/supervisor-3.4.0-1.el7.noarch.rpm

echo "install-3 rabbitmq..."
rpm -ivh DS-RPMS/lksctp-tools-1.0.17-2.el7.x86_64.rpm \
         DS-RPMS/erlang-asn1-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-compiler-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-crypto-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-erts-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-hipe-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-inets-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-kernel-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-mnesia-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-os_mon-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-otp_mibs-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-public_key-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-runtime_tools-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-sasl-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-sd_notify-0.1-1.el7.x86_64.rpm \
         DS-RPMS/erlang-snmp-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-ssl-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-stdlib-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-syntax_tools-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-tools-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/erlang-xmerl-R16B-03.18.el7.x86_64.rpm \
         DS-RPMS/rabbitmq-server-3.3.5-34.el7.noarch.rpm

echo "install-4 postgresql..."
rpm -ivh DS-RPMS/postgresql-libs-9.2.24-4.el7_8.x86_64.rpm \
         DS-RPMS/postgresql-9.2.24-4.el7_8.x86_64.rpm \
         DS-RPMS/postgresql-server-9.2.24-4.el7_8.x86_64.rpm

echo "install-5 redis..."
rpm -ivh DS-RPMS/redis-6.0.5-1.el7.remi.x86_64.rpm

echo "install-6 nginx..."
rpm -ivh DS-RPMS/nginx-1.18.0-1.el7.ngx.x86_64.rpm

echo "install-7 liberation-mono-fonts..."
rpm -ivh DS-RPMS/fontpackages-filesystem-1.44-8.el7.noarch.rpm \
         DS-RPMS/liberation-fonts-common-1.07.2-16.el7.noarch.rpm \
         DS-RPMS/liberation-mono-fonts-1.07.2-16.el7.noarch.rpm

echo "install-8 libXScrnSaver..."
rpm -ivh DS-RPMS/atk-2.28.1-2.el7.x86_64.rpm \
         DS-RPMS/libusbx-1.0.21-1.el7.x86_64.rpm \
         DS-RPMS/xkeyboard-config-2.24-1.el7.noarch.rpm \
         DS-RPMS/libxkbcommon-0.7.1-3.el7.x86_64.rpm \
         DS-RPMS/libgusb-0.2.9-1.el7.x86_64.rpm \
         DS-RPMS/libfontenc-1.1.3-3.el7.x86_64.rpm \
         DS-RPMS/libXfont2-2.0.3-1.el7.x86_64.rpm \
         DS-RPMS/libX11-common-1.6.7-2.el7.noarch.rpm \
         DS-RPMS/libXau-1.0.8-2.1.el7.x86_64.rpm \
         DS-RPMS/libxcb-1.13-1.el7.x86_64.rpm \
         DS-RPMS/libX11-1.6.7-2.el7.x86_64.rpm \
         DS-RPMS/libXext-1.3.3-3.el7.x86_64.rpm \
         DS-RPMS/libXrender-0.9.10-1.el7.x86_64.rpm \
         DS-RPMS/libXrandr-1.5.1-2.el7.x86_64.rpm \
         DS-RPMS/libXdmcp-1.1.2-6.el7.x86_64.rpm \
         DS-RPMS/libXScrnSaver-1.2.2-6.1.el7.x86_64.rpm

echo "install-9 pwgen..."
rpm -ivh DS-RPMS/pwgen-2.08-1.el7.x86_64.rpm

echo "install-10 dejavu-sans-fonts..."
rpm -ivh DS-RPMS/gsettings-desktop-schemas-3.28.0-3.el7.x86_64.rpm
rpm -ivh DS-RPMS/lcms2-2.6-3.el7.x86_64.rpm
rpm -ivh DS-RPMS/colord-libs-1.3.4-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/libwayland-client-1.15.0-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libwayland-cursor-1.15.0-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libwayland-server-1.15.0-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/mesa-libglapi-18.3.4-7.el7_8.1.x86_64.rpm
rpm -ivh DS-RPMS/mesa-libgbm-18.3.4-7.el7_8.1.x86_64.rpm
rpm -ivh DS-RPMS/libglvnd-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm
rpm -ivh DS-RPMS/libxshmfence-1.2-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libglvnd-egl-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm \
         DS-RPMS/mesa-libEGL-18.3.4-7.el7_8.1.x86_64.rpm
rpm -ivh DS-RPMS/libXfixes-5.0.3-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXdamage-1.1.4-4.1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXxf86vm-1.1.4-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libglvnd-glx-1.0.1-0.8.git5baa1e5.el7.x86_64.rpm \
         DS-RPMS/mesa-libGL-18.3.4-7.el7_8.1.x86_64.rpm
rpm -ivh DS-RPMS/dejavu-fonts-common-2.33-6.el7.noarch.rpm
rpm -ivh DS-RPMS/dejavu-sans-fonts-2.33-6.el7.noarch.rpm
echo "install-10 xorg-x11-server-Xvfb..."
rpm -ivh DS-RPMS/fontconfig-2.13.0-4.3.el7.x86_64.rpm
rpm -ivh DS-RPMS/pixman-0.34.0-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/cairo-1.15.12-4.el7.x86_64.rpm
rpm -ivh DS-RPMS/cairo-gobject-1.15.12-4.el7.x86_64.rpm
rpm -ivh DS-RPMS/dconf-0.28.0-4.el7.x86_64.rpm
rpm -ivh DS-RPMS/libepoxy-1.5.2-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/psmisc-22.20-16.el7.x86_64.rpm
rpm -ivh DS-RPMS/GConf2-3.2.6-8.el7.x86_64.rpm
rpm -ivh DS-RPMS/json-glib-1.4.2-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXcomposite-0.4.4-4.1.el7.x86_64.rpm
rpm -ivh DS-RPMS/adwaita-cursor-theme-3.28.0-1.el7.noarch.rpm
rpm -ivh DS-RPMS/adwaita-icon-theme-3.28.0-1.el7.noarch.rpm
rpm -ivh DS-RPMS/libxkbfile-1.0.9-3.el7.x86_64.rpm
rpm -ivh DS-RPMS/xorg-x11-xkb-utils-7.7-14.el7.x86_64.rpm
rpm -ivh DS-RPMS/xorg-x11-server-common-1.20.4-10.el7.x86_64.rpm
rpm -ivh DS-RPMS/hicolor-icon-theme-0.12-7.el7.noarch.rpm
rpm -ivh DS-RPMS/libwayland-egl-1.15.0-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libjpeg-turbo-1.2.90-8.el7.x86_64.rpm
rpm -ivh DS-RPMS/jbigkit-libs-2.0-11.el7.x86_64.rpm
rpm -ivh DS-RPMS/jasper-libs-1.900.1-33.el7.x86_64.rpm
rpm -ivh DS-RPMS/libtiff-4.0.3-32.el7.x86_64.rpm
rpm -ivh DS-RPMS/gdk-pixbuf2-2.36.12-3.el7.x86_64.rpm
rpm -ivh DS-RPMS/gtk-update-icon-cache-3.22.30-5.el7.x86_64.rpm
rpm -ivh DS-RPMS/libICE-1.0.9-9.el7.x86_64.rpm
rpm -ivh DS-RPMS/libSM-1.2.2-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXt-1.1.5-3.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXmu-1.1.2-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/xorg-x11-xauth-1.0.9-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/xorg-x11-server-Xvfb-1.20.4-10.el7.x86_64.rpm
echo "install-10 gtk3..."
rpm -ivh DS-RPMS/libXcursor-1.1.15-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libmodman-2.0.1-8.el7.x86_64.rpm
rpm -ivh DS-RPMS/libproxy-0.4.11-11.el7.x86_64.rpm
rpm -ivh DS-RPMS/nettle-2.7.1-8.el7.x86_64.rpm
rpm -ivh DS-RPMS/trousers-0.3.14-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/gnutls-3.3.29-9.el7_6.x86_64.rpm
rpm -ivh DS-RPMS/glib-networking-2.56.1-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libsoup-2.62.2-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/rest-0.8.1-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXinerama-1.1.3-2.1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXi-1.7.9-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXtst-1.2.3-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/at-spi2-core-2.28.0-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/at-spi2-atk-2.26.2-1.el7.x86_64.rpm
rpm -ivh DS-RPMS/avahi-libs-0.6.31-20.el7.x86_64.rpm
rpm -ivh DS-RPMS/cups-libs-1.6.3-43.el7.x86_64.rpm
rpm -ivh DS-RPMS/fribidi-1.0.2-1.el7_7.1.x86_64.rpm
rpm -ivh DS-RPMS/graphite2-1.3.10-1.el7_3.x86_64.rpm
rpm -ivh DS-RPMS/harfbuzz-1.7.5-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/libXft-2.3.2-2.el7.x86_64.rpm
rpm -ivh DS-RPMS/libthai-0.1.14-9.el7.x86_64.rpm
rpm -ivh DS-RPMS/pango-1.42.4-4.el7_7.x86_64.rpm
rpm -ivh DS-RPMS/gtk3-3.22.30-5.el7.x86_64.rpm

echo "install-11 documentserver..."
rpm -ivh DS-RPMS/onlyoffice-documentserver.x86_64.rpm
